<?php
namespace Common\Model;
use Common\Model\VRNewsModel;
class NewsVideoModel extends VRNewsModel
{
	
	protected $_validate = array(
		//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
		array('offer_price', 'require', '出价不能为空！', 0, 'regex', CommonModel:: MODEL_BOTH  ),
	);

	
	/**
	 * 上传视频
	 */
	public function uploadVideo($news_id = null, $is_vr = '0'){
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     899614161 ;// 设置附件上传大小
		$upload->exts      =     array('avi', 'mp4', 'rmvb', 'flv', 'asf', 'rm', '3gp', 'wmv', 'mpeg', 'mpg');// 设置附件上传类型
		$upload->rootPath  =     C('UPLOADPATH'); // 设置附件上传根目录
		$upload->savePath  =     ''; // 设置附件上传（子）目录
	
		// 上传文件
		$info = $upload->upload();
		if(!$info) {// 上传错误提示错误信息
			var_dump($upload->getError());exit;
		}else{
			$videoName = $info['video']['name'];
			$size = $info['video']['size'];
			$ext = $info['video']['ext'];
			$sha1 = $info['video']['sha1'];
			$savename = C('UPLOADPATH').$info['video']['savepath'].$info['video']['savename'];
			$config = array('SecretId'       => C("API_SecretId"),
					'SecretKey'      => C("API_SecretKey"),
					'RequestMethod'  => 'POST',
					'DefaultRegion'  => C("API_DefaultRegion"));
	
			vendor("QcloudApi.QcloudApi");
			$service = \QcloudApi::load(\QcloudApi::MODULE_VOD, $config);
			//要输入的参数
			$package = array(
					'fileName' => $savename,                                              //文件的绝对路径，包含文件名
					'dataSize' => $size,                                          //分片大小，建议使用默认值5MB
					'isTranscode' => 1,                                                  //是否转码
					'isScreenshot' => 1,                                                 //是否截图
					'isWatermark' => 0,                                                  //是否添加水印
					//'notifyUrl' =>C("notify_url")."&type=1&is_vr=".$is_vr."&key=".sha1(md5(".$news_id.")+"_xhw")              //转码完成后的回调地址，不转码此项无效
					'notifyUrl' =>$_SERVER['SERVER_NAME'].'/'.C("notify_url")."&type=1&is_vr=".$is_vr."&key=".sha1(md5(".$news_id.")+"_xhw")
					//'notifyUrl' =>"http://congcong.p.imooc.io/?key=123"
			);
	
			$a = $service->MultipartUploadVodFile($package);;
			\Think\Log::record('腾讯云返回：'.json_encode($a), 'INFO');
			if($a == false){
				return false;
				//return $this->error("调用腾讯云接口错误：".$service->getError()->getMessage());
			}else{
	
				$fileId = $a['fileId'];
				//删除本地文件
				unlink($savename);
				return $fileId;
			}
		}
	}

// 	/**
// 	 * 上传视频（新视频）
// 	 */
// 	public function uploadVideo($fileName = null, $fileSize=null, $fileDir = null, $news_id = null, $is_vr = '0'){
// 		if($fileName && $fileSize && $fileDir){
// 			$save_dir = $_SERVER['DOCUMENT_ROOT'].'/'.C('UPLOADPATH').$fileDir;
// 			$savename = C('UPLOADPATH').$fileDir.'/'.$fileName;
// 			$config = array('SecretId'       => C("API_SecretId"),
// 					'SecretKey'      => C("API_SecretKey"),
// 					'RequestMethod'  => 'POST',
// 					'DefaultRegion'  => C("API_DefaultRegion"));
			
// 			vendor("QcloudApi.QcloudApi");
// 			$service = \QcloudApi::load(\QcloudApi::MODULE_VOD, $config);
// 			//要输入的参数
// 			$package = array(
// 					'fileName' => $savename,                                              //文件的绝对路径，包含文件名
// 					'dataSize' => $fileSize,                                          //分片大小，建议使用默认值5MB
// 					'isTranscode' => 1,                                                  //是否转码
// 					'isScreenshot' => 1,                                                 //是否截图
// 					'isWatermark' => 0,                                                  //是否添加水印
// 					//'notifyUrl' =>C("notify_url")."&type=1&is_vr=".$is_vr."&key=".sha1(md5(".$news_id.")+"_xhw")              //转码完成后的回调地址，不转码此项无效
// 					'notifyUrl' =>C("notify_url")."&type=1&is_vr=".$is_vr."&key=".sha1(md5(".$news_id.")+"_xhw")
// 					//'notifyUrl' =>"http://congcong.p.imooc.io/?key=123"
// 			);
// 			$a = $service->MultipartUploadVodFile($package);
// 			\Think\Log::record('腾讯云返回：'.json_encode($a), 'INFO');
// 			if($a == false){
// 				return false;
// 				//return $this->error("调用腾讯云接口错误：".$service->getError()->getMessage());
// 			}else{
			
// 				$fileId = $a['fileId'];
// 				//删除本地文件夹
// 				$this->deldir($save_dir);
// 				return $fileId;
// 			}
// 		}
// 	}
	
	
	public function deldir($dir) {
		//先删除目录下的文件：
		$dh=opendir($dir);
		while ($file=readdir($dh)) {
			if($file!="." && $file!="..") {
				$fullpath=$dir."/".$file;
				if(!is_dir($fullpath)) {
					unlink($fullpath);
				} else {
					deldir($fullpath);
				}
			}
		}
	
		closedir($dh);
	  //删除当前文件夹：
		if(rmdir($dir)) {
		  return true;
		} else {
		  return false;
	    }
	}
	
	public function get_video($fileName = null){
		$data = array();
		if($fileName){
			$config = array('SecretId'       => C("API_SecretId"),
					'SecretKey'      => C("API_SecretKey"),
					'RequestMethod'  => 'GET',
					'DefaultRegion'  => C("API_DefaultRegion"));
			vendor("QcloudApi.QcloudApi");
			$service = \QcloudApi::load(\QcloudApi::MODULE_VIDEO, $config);
			//要输入的参数
			//14651978969340560036
			$package = array(
					'fileName'=>$fileName
			);
			$data = $service->DescribeVodPlayInfo($package);
		}
		return $data;
	}
	
}

